(function () {

	'use strict';

	angular
		.module('app')
		.factory('CartModel', CartModel);

	CartModel.$inject = ['$http', '$q', '$timeout', '$rootScope', 'CacheFactory', 'AppAuthenticationService', 'API', 'ENUM'];

	function CartModel($http, $q, $timeout, $rootScope, CacheFactory, AppAuthenticationService, API, ENUM) {

		var service = {};
		service.isEmpty = false;
		service.isLoaded = false;
		service.isLoading = false;
		service.isLastPage = false;
		service.total = 0;
		service.carts = null;
		service.subtotal = {};

		service.add = function(goodsID, num) {
            if (!AppAuthenticationService.getToken())
                return;

            var _this = this;
            return API.cart.addCart({goods_id: goodsID, num: num}).then(function (res) {
				if (res) {
					_this.reload();
				}
			});
        };

		service.delete = function(cartID) {
            if (!AppAuthenticationService.getToken())
                return;

            var _this = this;
            return API.cart.deleteCart({cart_id: cartID}).then(function (res) {
				if (res) {
                    var carts = _this.carts;
                    for (var i = 0; i < carts.length; ++i) {
                        var cart = carts[i];
                        if (cart.cart_id == cartID) {
                            _this.total = _this.total-cart.num>0?_this.total-cart.num:0;
                            carts.splice(i, 1);
                            break;
                        }
                    }
                    _this.isEmpty = (_this.carts && _this.carts.length) ? false : true;
				}
				return true;
			});
        };

		service.update = function(cartID, num) {
            if (!AppAuthenticationService.getToken())
                return;

            var _this = this;
            return API.cart.updateNum({cart_id: cartID, num: num}).then(function (res) {
                if (res) {
                    var carts = _this.carts;
                    for (var i = 0; i < carts.length; ++i) {
                        var cart = carts[i];
                        if (cart.cart_id == cartID) {
                            cart.num = cart.num + num;
                            break;
						}
                    }
                    _this.isEmpty = (_this.carts && _this.carts.length) ? false : true;
                }
            });
        };

        service.getTotal = function() {
            API.cart.getCartNum({}).then(function (res) {
                if (res > 0) {
                    service.total = res;
                }
            });
        }

		service.reload = _reload;
		service.reloadIfNeeded = _reloadIfNeeded;
		return service;

        function _reload() {
            var _this = this;
			if (!AppAuthenticationService.getToken()) {
                _this.total = 0;// 置空
				return;
			}
            API.cart.cartList({}).then(function (res) {
                _this.getTotal();
                _this.carts = res.rows;
                _this.isEmpty = res.total>0?false:true;
                _this.isLoaded = true;
                _this.isLoading = false;

                API.order.getSubTotal().then(function (res) {
                    _this.subtotal = res;
                });
            });
		}

		function _reloadIfNeeded() {
			// if (!this.isLoaded) {
            this.reload();
			// }
		}
	}

})();